var subCategory={
	    "Books": {
	        "1": "Academic",
	        "2": "Technical",
	        "3": "Entrance_Exam",
	        "4": "Literature__Fiction",
	        "5": "Indian_Writing",
	        "6": "New_Releases",
	        "7": "Bestsellers"
	    },
	    "Mobiles": {
	        "8": "Nokia",
	        "9": "Samsung",
	        "10": "Apple",
	        "11": "HTC",
	        "12": "Micromax",
	        "13": "Sony",
	        "14": "Karbonn"
	    },
	    "Electronics": {
	        "15": "Irons",
	        "16": "Fans",
	        "17": "Cameras",
	        "18": "Dryers",
	        "19": "Kettles",
	        "20": "Torch_lights",
	        "21": "Juicers_and_Blenders"
	    },
	    "Jobs": {
	        "22": "Departmental_Projects",
	        "23": "Start-up_Projects",
	        "24": "TA_Notifications",
	        "25": "Jobs_on_existing projects"
	    },
	    "Rent Taxi": {
	        "26": "Prepaid",
	        "27": "Shared",
	        "28": "Postpaid",
	        "29": "Private"
	    },
	    "Educations": {
	        "30": "Extra_Courses",
	        "31": "Clubs",
	        "32": "Programming_Classes"
	    },
	    "Pets": {

	    },
	    "Events": {
	        "33": "Grubs",
	        "34": "Cultural_Nights",
	        "35": "Music_Nights",
	        "36": "Club_Events"
	    },
	    "Cloths": {
	        "37": "T-Shirts",
	        "38": "Dresses",
	        "39": "Jeans",
	        "40": "Formal_Shirts",
	        "41": "Kurtis",
	        "42": "Suits_n_Blazers"
	    },
	    "Computers": {
	        "43": "Dell",
	        "44": "Sony_Vaio",
	        "45": "Hp",
	        "46": "Lenovo",
	        "47": "Compaq"
	    },
	    "Bicycles": {
	        "48": "Ladies",
	        "49": "Second_hand",
	        "50": "Brand_new"
	    },
	    "Movies": {
	        "51": "Hindi_Movies_n_TV shows",
	        "52": "English_Movies_n_TV shows",
	        "53": "New_releases",
	        "54": "DVDs"
	    }
	};


$(document).ready(function() {


});


//Post View 
function loadPostView(object, url) {
	
	$.ajax({
		'type' : 'POST',
		'cache' : false,
		'contentType' : 'application/x-www-form-urlencoded; charset=UTF-8',
		'url' : url,
        'async': false,
		success : function(data) {
			$('#body').html(' ');
			$('#body').html(data);	
			
			/* Added by Rachit to do cool stuff*/
			$('#bbHome').removeClass("active");
			$('#bbAbout').removeClass("active");
			$('#bbContact').removeClass("active");
			$('#bbCategory').removeClass("active");
			$('#bbSignUp').removeClass("active");
			$('#bbLoggedUser').removeClass("active");
			$('#bbLogin').removeClass("active");
			$('#bbLogout').removeClass("active");
			if(url=="home.html"){
				$('#bbHome').addClass("active");
			}
			else if(url=="aboutUs.html"){
				$('#bbAbout').addClass("active");
			}
			else if(url=="contact.html"){
				$('#bbContact').addClass("active");
				
			}
			else if(url=="buyPage.html"){
				$('#bbCategory').addClass("active");
			}
			else if(url=="signUp.html"){
				$('#bbSignUp').addClass("active");
			}
			else if(url=="login.html"){
				$("#bbLoggedUser").addClass("active");
				$("#bbLogin").addClass("active");
			}
			else if(url=="logout.html"){
				$("#bbLogout").addClass("active");
			}
			
			/* Added By Nishant: This will show subcategory after ading create add form in Container */
		    $("#inputCategory").change(function () {
		        var selectedVal = $("#inputCategory option:selected").text();


		        if(selectedVal != "Select Category"){
		        	$('#subCategoryList').show();
		            $('#subCategory').empty();

		            var subCatList = subCategory[selectedVal];

		            for(key in subCatList){
		                $('#subCategory').append("<option value='" + key + "'>" + subCatList[key] + "</option>");
		            }
		        }else{
		        	$('#subCategoryList').hide();
		            $('#subCategory').empty();
		        }      
		    });
		    
		    /* Have to add click event on category list that will show subcategory */
            $('#Electronics, #Movies,#Bicycles,#Computers,#Cloths,#Events,#Pets,#Educations,#Rent Taxi,#Jobs, #Mobiles, #Books').click(function(){
                // Iterate through all anchor tag and bind click event which will take user to sub cat list
                   
                    // This will give category name
                    var selectedCategory = $(this).attr('id');
                    var objSubCatList = subCategory[selectedCategory];
                    var subCatListRow1 = $('#subCatListRow1');
                    var subCatListRow2 = $('#subCatListRow2');
                    var count = 0 ;
                    
                    
                    
                    // Empty subCategory List
                    $('#subCatListRow2').empty();
                    $('#subCatListRow1').empty();

                    // Create subCat List
                    for(key in objSubCatList) {
                        var str = "<div class='span2' id='" + objSubCatList[key] + "'>" +
                                        "<a id='" + key +"' href='#'><img alt='' src='img/" + objSubCatList[key] + ".png'><span>" + objSubCatList[key] + "</span></a>" +
                                  "</div>";

                        if (count < 6) {
                            subCatListRow1.append(str);
                        }else{
                            subCatListRow2.append(str)
                        }
                        count++;

                        // Add click event at the same time
                        $("#" + key).click(function(){
                            var catId = $(this).attr('id');
                            $.ajax({
                                'type' : 'GET',
                                'url' : "product_list/" + catId+".html",
                                success : function(data) {
                                    $('#body').html(' ');
                                    $('#body').html(data);

                                    // This method is added by nishant for adding event to the product list
                                    $('#productList').find('a').each(function(e) {
                                        $(this).click(function(){
                                            var catId = $(this).attr('id');
                                            $.ajax({
                                                'type' : 'GET',
                                                'url' : "product/" + catId+".html",
                                                success : function(data) {
                                                    $('#body').html(' ');
                                                    $('#body').html(data);
                                                },
                                                error : function(xhr, status, err) {
                                                    alert("error : " + err + "status" + status + "  xhr : "
                                                        + xhr.status);
                                                }
                                            });
                                        });
                                    });
                                },
                                error : function(xhr, status, err) {
                                    alert("error : " + err + "status" + status + "  xhr : "
                                        + xhr.status);
                                }
                            });
                        });
                    }
               
            });

		    
		},
		error : function(xhr, status, err) {
			alert("error : " + err + "status" + status + "  xhr : "
					+ xhr.status);
		}
	});
	
	return true;
}

function loadView(object, url) {
		
	$.ajax({
		'type' : 'GET',
		'cache' : false,
		'contentType' : 'application/x-www-form-urlencoded; charset=UTF-8',
		'url' : url,
        'async': false,
		success : function(data) {
			$('#body').html(' ');
			$('#body').html(data);	
			
			/* Added by Rachit to do cool stuff*/
			$('#bbHome').removeClass("active");
			$('#bbAbout').removeClass("active");
			$('#bbContact').removeClass("active");
			$('#bbCategory').removeClass("active");
			$('#bbSignUp').removeClass("active");
			$('#bbLoggedUser').removeClass("active");
			$('#bbLogin').removeClass("active");
			$('#bbLogout').removeClass("active");
			if(url=="home.html"){
				$('#bbHome').addClass("active");
			}
			else if(url=="aboutUs.html"){
				$('#bbAbout').addClass("active");
			}
			else if(url=="contact.html"){
				$('#bbContact').addClass("active");
				
			}
			else if(url=="buyPage.html"){
				$('#bbCategory').addClass("active");
			}
			else if(url=="signUp.html"){
				$('#bbSignUp').addClass("active");
			}
			else if(url=="login.html"){
				$("#bbLoggedUser").addClass("active");
				$("#bbLogin").addClass("active");
			}
			else if(url=="logout.html"){
				$("#bbLogout").addClass("active");
			}
			
			/* Added By Nishant: This will show subcategory after ading create add form in Container */
		    $("#inputCategory").change(function () {
		        var selectedVal = $("#inputCategory option:selected").text();


		        if(selectedVal != "Select Category"){
		        	$('#subCategoryList').show();
		            $('#subCategory').empty();

		            var subCatList = subCategory[selectedVal];

		            for(key in subCatList){
		                $('#subCategory').append("<option value='" + key + "'>" + subCatList[key] + "</option>");
		            }
		        }else{
		        	$('#subCategoryList').hide();
		            $('#subCategory').empty();
		        }      
		    });
		    
		    /* Have to add click event on category list that will show subcategory */
            $('#Electronics, #Movies,#Bicycles,#Computers,#Cloths,#Events,#Pets,#Educations,#Rent Taxi,#Jobs, #Mobiles, #Books').click(function(){
                // Iterate through all anchor tag and bind click event which will take user to sub cat list
                   
                    // This will give category name
                    var selectedCategory = $(this).attr('id');
                    var objSubCatList = subCategory[selectedCategory];
                    var subCatListRow1 = $('#subCatListRow1');
                    var subCatListRow2 = $('#subCatListRow2');
                    var count = 0 ;
                    
                    
                    
                    // Empty subCategory List
                    $('#subCatListRow2').empty();
                    $('#subCatListRow1').empty();

                    // Create subCat List
                    for(key in objSubCatList) {
                        var str = "<div class='span2' id='" + objSubCatList[key] + "'>" +
                                        "<a id='" + key +"' href='#'><img alt='' src='img/" + objSubCatList[key] + ".png'><span>" + objSubCatList[key] + "</span></a>" +
                                  "</div>";

                        if (count < 6) {
                            subCatListRow1.append(str);
                        }else{
                            subCatListRow2.append(str)
                        }
                        count++;

                        // Add click event at the same time
                        $("#" + key).click(function(){
                            var catId = $(this).attr('id');
                            $.ajax({
                                'type' : 'GET',
                                'url' : "product_list/" + catId+".html",
                                success : function(data) {
                                    $('#body').html(' ');
                                    $('#body').html(data);

                                    // This method is added by nishant for adding event to the product list
                                    $('#productList').find('a').each(function(e) {
                                        $(this).click(function(){
                                            var catId = $(this).attr('id');
                                            $.ajax({
                                                'type' : 'GET',
                                                'url' : "product/" + catId+".html",
                                                success : function(data) {
                                                    $('#body').html(' ');
                                                    $('#body').html(data);
                                                },
                                                error : function(xhr, status, err) {
                                                    alert("error : " + err + "status" + status + "  xhr : "
                                                        + xhr.status);
                                                }
                                            });
                                        });
                                    });
                                },
                                error : function(xhr, status, err) {
                                    alert("error : " + err + "status" + status + "  xhr : "
                                        + xhr.status);
                                }
                            });
                        });
                    }
               
            });

		    
		},
		error : function(xhr, status, err) {
			alert("error : " + err + "status" + status + "  xhr : "
					+ xhr.status);
		}
	});
	
	return true;
}

//Submit validation

function submitValidation(productid, Url) 
{
	var value="id="+productid;
	$.ajax({
	    type: "POST",
	    cache:false,
	 	async : false,
	    url: Url,
	 	contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
	    data: value,
	    success : function(data){
	    	$('#body').html(' ');
			$('#body').html(data);
	    }
	});
}


//using FormData() object
/*function uploadFormData(){
  var oMyForm = new FormData();
  oMyForm.append("file", file2.files[0]);
  oMyForm.append("file", file2.files[1]);
  $.ajax({
    url: 'BitsBazaar/upload',
    data: oMyForm,
    dataType: 'text',
    processData: false,
    contentType: false,
    type: 'POST',
    success: function(data){
       $('#body').html('');
      $('#body').html(data);
    }
  });
  return false;
}*/

function showsubcat(){
	var value = $("#subcatworks");
	$.ajax({
	    type: "POST",
	    url: "registerUser.html",
	    data: value,
	    success : function(data){
	    	$('#body').html(' ');
			$('#body').html(data);
	    }
	});
}

function validateChangePwd(){
    var newPwd = $('#newInputPassword').val();
    var confNewPwd = $('#oldInputPassword').val();
    
    var isValidData = true;
    var errMessage = "Please enter valid data in highlighted field and try again.";
    
    if (newPwd == "") {
    	$('#newInputPassword').css('border-color','red');
        isValidData = false;
    }else{
    	$('#newInputPassword').css('border-color','#cccccc');
    }

    if(newPwd.length < 6){
    	$('#newInputPassword').css('border-color','red');
    	errMessage = "Hey Minimum password length should be 6 characters ";
    	isValidData = false;
    }else {
    	$('#pwd').css('border-color','#cccccc');
    }
    
    if (confNewPwd == "") {
    	$('#oldInputPassword').css('border-color','red');
        isValidData = false;
    }else{
    	$('#oldInputPassword').css('border-color','#cccccc');
    }
}

//Change pwd method 
function submitChangepassword(){
	if(validateChangePwd()){
		// Ankit will add ajax call 
	}
	
	return false;
}

function submitSignUp(){
	
	if(validateSignUpField()){
		var value = $("#signUpForm").serialize();
		alert(value);
		$.ajax({
		    type: "POST",
		    url: "registerUser.html",
		    data: value,
		    success : function(data){
		    	$('#body').html(' ');
				$('#body').html(data);
		    }
		});
	}
}

function submitLogin(formId, Url) 
{
	if(validateSignIn()){
		var value = $("#" + formId).serialize();
		
		alert('darshan');
		$.ajax({
			'type' : "POST",
			'cache' : false,
			'contentType' : 'application/x-www-form-urlencoded; charset=UTF-8',
			'async' : false,
			'url' : Url,
			'data' : value,
			'success' : function(data) {
				$('#body').html(' ');
				$('#body').html(data);
			}
		});
		//var fromJavaSide = document.getElementById("redirectLogin").value;//'${errorJs}';
		//alert(fromJavaSide);
		if($('#loginError').text() == ""){
		   location.reload(true);
		}
	}
	return false;
}


function logout(object, Url) {
	$.ajax({
		'type' : 'GET',
		'cache' : false,
		'contentType' : 'application/x-www-form-urlencoded; charset=UTF-8',
		'url' : Url,
		'async' : false,
		success : function(data) {
			$('#body').html(' ');
			$('#body').html(data);

		},
		error : function(xhr, status, err) {
			alert("error : " + err + "status" + status + "  xhr : "
					+ xhr.status);
		}
	});
	location.reload(true);
	return true;
}


function validateSignUpField() {

    var fullName = $('#fullName').val();
    var emailAdd = $('#emailAdd').val();
    var mobNo = $('#mobNo').val();
    var address = $('#address').val();
    var password = $('#pwd').val();
    var confPassword = $('confPwd').val();

    function IsValidEmailAdd(uEmail) {
        //Validate email address
        var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

        return re.test(uEmail);
    }

    var isValidData = true;
    var errMessage = "Please enter valid data in highlighted field and try again.";

    if (fullName == "") {
    	$('#fullName').css('border-color','red');
        isValidData = false;
    }else{
    	$('#fullName').css('border-color','#cccccc');
    }
    
    if (emailAdd == "" || !IsValidEmailAdd(emailAdd)) {
    	$('#emailAdd').css('border-color','red');
        isValidData = false;
    }else{
    	$('#emailAdd').css('border-color','#cccccc');
    }

    if (mobNo == "") {
    	$('#mobNo').css('border-color','red');
        isValidData = false;
    }else{
    	$('#mobNo').css('border-color','#cccccc');
    }

    if (address == "") {
    	$('#address').css('border-color','red');
        isValidData = false;
    }else{
    	$('#address').css('border-color','#cccccc');
    }

    if (password == "") {
    	$('#pwd').css('border-color','red');
        isValidData = false;
    }else{
    	$('#pwd').css('border-color','#cccccc');
    }

    if(password.length < 6){
    	$('#pwd').css('border-color','red');
    	errMessage = "Hey Minimum password length should be 6 characters ";
    	 isValidData = false;
    }else {
    	$('#pwd').css('border-color','#cccccc');
    }
    
    if (confPassword == "") {
    	$('#confPwd').css('border-color','red');
        isValidData = false;
    }else{
    	$('#confPwd').css('border-color','#cccccc');
    }

   /* if (confPassword != password) {
    	$('#confPwd').css('border-color','red');
    	$('#pwd').css('border-color','red');
        isValidData = false;
    }else{
    	$('#confPwd').css('border-color','#cccccc');
    	$('#pwd').css('border-color','#cccccc');
    }*/

    if (!isValidData) {
        $('#errMessageDiv').show().html(errMessage);
    } else {
        $('#errMessageDiv').hide();
    }

    return isValidData;
}


function validateSignIn(){

    var emailAdd = $('#inputEmail').val();
    var password = $('#inputPassword').val();

    function IsValidEmailAdd(uEmail) {
        //Validate email address
        var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

        return re.test(uEmail);
    }
    
    var isValidData = true;
    var errMessage = "Oops!! You forgot to enter valid data.";
    
    if (emailAdd == "" || !IsValidEmailAdd(emailAdd)) {
    	$('#inputEmail').css('border-color','red');
        isValidData = false;
    }else{
    	$('#inputEmail').css('border-color','#cccccc');
    }
    
    if (password == "") {
    	$('#inputPassword').css('border-color','red');
        isValidData = false;
    }else{
    	$('#inputPassword').css('border-color','#cccccc');
    }
    
    if(isValidData){
    	$('#errMessageDiv').hide();
    }else{
    	$('#errMessageDiv').show().html(errMessage);
    }
    
    return isValidData;
}


